﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Shell;


namespace ScannerDemo
{
    public partial class Detail : PhoneApplicationPage
    {
        /// <summary>
        /// Declaration Variable
        /// </summary>
        /// 

        WebClient client;
        App App = App.Current as App;
        int QueryNumber;
        ItemList ItemData;
        public Detail()
        {
            
            InitializeComponent();
            
            itemid.Text = "ITEM ID : " + App.itemId;
            //MessageBox.Show("Decode : " + App.itemId);

            /// HTTP Request
            client = new WebClient();
            string url = "http://localhost/mobile/Showitem.php";
            //string url = "http://192.168.1.105/mobile/Showitem.php";
            client.DownloadStringAsync(new Uri(url));
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        }
        /// Read & Check Data From XML
        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                XElement twitterElements = XElement.Parse(e.Result);

                var postList =
                    from tweet in twitterElements.Descendants("item")
                    where tweet.Element("id").Value == App.itemId
                    select  ItemData = new ItemList
                    {
                        Id = tweet.Element("id").Value,
                        Name = tweet.Element("itemname").Value,
                        Picture = tweet.Element("picture").Value,
                        Price = tweet.Element("price").Value,
                        Prices = tweet.Element("price").Value
                

                    };
                QueryNumber = postList.Count();
                if (QueryNumber == 1)
                {
                    App.StateId = ItemData.Prices;
                    App.StateName = ItemData.Name;
                    App.StatePicture = ItemData.Picture;
                    App.StatePrice = ItemData.Price;
                    App.StateQuantity = Quantity.Text;


                    Name.Text = ItemData.Name;
                    Price.Text = "x " + ItemData.Price;
                    imageitem.Source = new BitmapImage(new Uri(App.StatePicture, UriKind.RelativeOrAbsolute));

                }
                else
                {
                    MessageBox.Show("Not Found");

                    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                }

            }


        }
        /// <summary>
        /// ADD DATA TO LIST
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buybutton_Click(object sender, RoutedEventArgs e)
        {
            if (QueryNumber == 1)
            {
                float PriceS, QuantityS;
                if (float.TryParse(ItemData.Prices, out PriceS) && float.TryParse(Quantity.Text, out QuantityS)) 
                {
                            ItemData.Quantity = Quantity.Text;
                           // App.totalPrice += (PriceS * QuantityS);
                            App.GlobalShopList.Add(ItemData);

                           // panoramaControl.DefaultItem = panoramaControl.Items[indexToSet];
                            NavigationService.Navigate(new Uri("/Panorama.xaml", UriKind.Relative));
                }
                else
                {
                    MessageBox.Show("Invalid input");
                }
                
            }
            
        }

    }
}